﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CLibArtreve.bll;
using CLibArtreve.security;

namespace WebAppArtreve.views.shared.components.categories
{
    public partial class WUCAddCategory : System.Web.UI.UserControl
    {
        private string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //BUTTON (ADD CATEGORY) CLICKED
        protected void linkAddCategory_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                AddNewCategory();
            }
        }

        //ADD A NEW CATEGORY
        private void AddNewCategory()
        {
            try
            {
                //1. BLL
                //-----------------------
                CategoryBLL bllCategory = new CategoryBLL(_connectionString);

                //2. INSERT CATEGORY IN MODEL
                //-----------------------
                CLibArtreve.models.Category category = new CLibArtreve.models.Category();
                category.Name = this.txtCategoryName.Text.Trim();
                category.Description = this.txtCategoryDescription.Text.Trim();
                category.ImgUrl = UploadFile(); //UPLOAD THE PICTURE

                //3. INSERT MODEL IN DB
                //-----------------------
                bllCategory.InsertArt(category);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        //UPLOAD THE FILE
        private string UploadFile()
        {
            //1. FILE SELECTED

            if (this.FileUploadComponentAjax.HasFile)
            {
                //2. FILENAME
                string fileName = Server.HtmlEncode(this.FileUploadComponentAjax.FileName);

                //3. GET EXTENSION OF FILE
                string fileExtension = System.IO.Path.GetExtension(fileName);

                //4. CHECK EXTENSION
                if (fileExtension == ".jpg" ||
                    fileExtension == ".png")
                {
                    //5. CHECK FILSEIZE
                    int fileSize = this.FileUploadComponentAjax.PostedFile.ContentLength;
                    if (fileSize <= 1024 * 1024)
                    {

                        //6. CREATE TARGET PATH
                        string fileNameInDatabase = fileName.Split('.')[0] + getTimeStamp() + fileExtension;
                        string targetFileName = ConfigurationManager.AppSettings["DMSPATHCAT"] + fileNameInDatabase;
                        targetFileName = Server.MapPath(targetFileName);

                        //7. SAVE THE FILE
                        this.FileUploadComponentAjax.SaveAs(targetFileName);


                        //Convert.ToString();
                        return fileNameInDatabase;
                        //lblResult.Text = "File uploaded";
                    }
                    else
                    {
                        //lblResult.Text = "Please choose a smaller file, otherwise fuck off.";
                        return null;
                    }
                }
                else
                {
                    //lblResult.Text = "Please choose a jpg or a png file, otherwise...";
                    return null;
                }
            }
            else
            {
                // lblResult.Text = "Please select a file, otherwise fuck off.";
                return null;
            }
        }

        //GET CURRENT TIMESTAMP
        private string getTimeStamp()
        {
            var nowDate = DateTime.Now;
            return nowDate.ToString("yyyyMddHHmmssffff");
        }
    }    
}